package com.splic.api.controller.product;

import cn.hutool.json.JSONObject;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.product.domain.VoiceTransfer;
import com.splic.product.service.ProductService;
import com.splic.product.service.VoiceTransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 转写事件Controller
 * 
 * @author singe
 * @date 2023-08-17
 */
@RestController
@RequestMapping("/api/transfer")
@Slf4j
public class ApiVoiceTransferController extends BaseController
{

    @Autowired
    private VoiceTransferService voiceTransferServiceImpl;
    @Autowired
    private ProductService productServiceImpl;

    /**
     * 是否允许转写
     *
     * @return
     */
    @GetMapping("/judgeTransfer")
    public ResponseObj<VoiceTransfer> judgeTransfer(VoiceTransfer transferReq) {
        try{
            VoiceTransfer transfer = voiceTransferServiceImpl.judgeTransfer(transferReq);
            return ResponseObj.SUCCESS(transfer);
        } catch (BusinessException e){
            if (e.getCode() != -1) {
                return ResponseObj.ERROR(e.getMessage());
            } else {
                return ResponseObj.ERROR(e.getMessage(), String.valueOf(e.getCode()));
            }
        } catch (Exception e){
            log.error("转写判断失败:{}",e);
            return ResponseObj.ERROR("转写判断失败。");
        }
    }

    /**
     * 更新转写
     *
     * @return
     */
    @GetMapping("/updateTransfer")
    public ResponseObj<Integer> updateTransfer(VoiceTransfer transferReq) {
        try{
            return ResponseObj.SUCCESS(voiceTransferServiceImpl.updateTransfer(transferReq));
        } catch (Exception e){
            log.error("更新转写失败:{}",e);
            return ResponseObj.ERROR("更新转写失败。");
        }
    }

    /**
     * 讯飞转写回调
     *
     * @return
     */
    @GetMapping("/callbackTransfer")
    public ResponseObj callbackTransfer(@RequestParam("orderId") String orderId,
                                                       @RequestParam("status") String status) {
        try{
            voiceTransferServiceImpl.callbackTransfer(orderId, status);
            return ResponseObj.SUCCESS();
        } catch (Exception e){
            log.error("回调失败:{}",e);
            return ResponseObj.ERROR("回调失败。");
        }
    }

    /**
     * Deepgram转写回调
     *
     * @return
     */
    @PostMapping("/callbackDeepgram")
    public ResponseObj callbackDeepgram(@RequestBody JSONObject response) {
        try{
            voiceTransferServiceImpl.callbackDeepgram(response);
            return ResponseObj.SUCCESS();
        } catch (Exception e){
            log.error("回调失败:{}",e);
            return ResponseObj.ERROR("回调失败。");
        }
    }

    /**
     * 查询转写事件列表
     */
    @GetMapping("/searchUserList")
    public ResponseObj<List<VoiceTransfer>> searchUserList()
    {
        try{
            return ResponseObj.SUCCESS(voiceTransferServiceImpl.searchUserList());
        } catch (Exception e){
            log.error("查询转写事件列表失败:{}",e);
            return ResponseObj.ERROR("查询转写事件列表失败。");
        }
    }

    /**
     * 查询转写事件
     */
    @GetMapping("/searchByTransferId")
    public ResponseObj<VoiceTransfer> searchById(String transferId)
    {
        try{
            return ResponseObj.SUCCESS(voiceTransferServiceImpl.apiVoiceTransferById(transferId));
        } catch (Exception e){
            log.error("查询转写事件失败:{}",e);
        return ResponseObj.ERROR("查询转写事件失败。");
}
    }

}
